package com.itheima.reggie.common;

import com.mysql.cj.jdbc.exceptions.MysqlDataTruncation;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLNonTransientException;

//全局异常通知类
@RestControllerAdvice
public class GlobalExceptionHandle {

    @ExceptionHandler
    public R handle1(SQLException e){
        //打印错误信息
        e.printStackTrace();
        //判断错误信息
        if (e.getMessage().contains("Duplicate entry")){
            //返回错误信息给前端展示
            return R.error(e.getMessage().split(" ")[2]+"账号已存在");
        }
        if (e.getMessage().contains("Data too long")){
            //返回错误信息给前端展示
            return R.error(e.getMessage().split(" ")[7]+"数据长度过长");
        }
        return R.error("未知错误");
    }

    @ExceptionHandler
    public R handle2(CustomerException c){
        //打印错误信息
        c.printStackTrace();
        return R.error(c.getMessage());
    }
}
